package com.cjy.sql;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;

/**
 * 过滤文件后缀名
 *
 * @author cjy
 * @create 2020-05-08
 */
public class FindLatsetFile {
    public static void main(String[] args) {
        getLogFile();
    }

    public static void getLatestFile() {
        String pathName = "E:\\workspace_directory\\Datax-web\\target\\datax\\datax\\log\\2020-04-14\\job";
        File path = new File(pathName);
        //列出该目录下所有百文件和文度件夹
        File[] files = path.listFiles();
        //按照文件最后修改日期知倒序排序
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return (int) (file2.lastModified() - file1.lastModified());
            }
        });
        //取出第一个(即最新回修改的)文件，打印文件名答
        System.out.println(files[0].getName());


        String[] fileList = path.list();


    }

    public static String getLogFile() {
        String pathName = "E:\\workspace_directory\\Datax-web\\target\\datax\\datax\\log\\2020-04-14\\job";
        File path = new File(pathName);
        //列出该目录下所有百文件和文度件夹
//        File[] files = path.listFiles();
        String[] fileList = path.list(new LogFilenameFilter(".log"));

        // lamda
        File[] files = path.listFiles(pathname -> (pathname.getName().endsWith(".log")&&pathname.isFile()));
        // LogFileFilter过滤器用于 listFiles 函数
        //File[] files = path.listFiles(new LogFileFilter(".log"));
        for (File file : files) {
            System.out.println("合格文件：" + file.getName());
        }
        //按照文件最后修改日期知倒序排序
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return (int) (file2.lastModified() - file1.lastModified());
            }
        });

        //
        // Arrays.sort(files, (file1, file2) -> (int) (file2.lastModified() - file1.lastModified()));
        //取出第一个(即最新回修改的)文件，打印文件名答
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("最新的文件：" + files[0].getName() + " path：" + files[0].getPath() + "modifierdate:" + sdf.format(files[0].lastModified()));

       /* FilenameFilter filenameFilter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith(".log")) {
                    return true;
                } else {
                    return true;
                }
            }
        };


        String[] fileList = path.list(filenameFilter);
*/
        return files[0].getPath();
    }


    static class LogFilenameFilter implements FilenameFilter {
        // type为需要过滤的条件，比如如果type=".jpg"，则只能返回后缀为jpg的文件
        private String type;

        public LogFilenameFilter(String type) {
            this.type = type;
        }

        @Override
        // 返回true的文件则合格
        public boolean accept(File dir, String name) {
            return name.endsWith(type);
        }
    }


    static class LogFileFilter implements FileFilter {
        private String type;

        public LogFileFilter(String type) {
            this.type = type;
        }

        @Override
        public boolean accept(File pathname) {
            // 这里如果需要使用文件的功能直接使用即可
            return pathname.getName().endsWith(type) && pathname.isFile();
        }


    }
}
